<?
/**
 * Copyright 2007 Melange.
 *
 * This file is part of PHP-MELANGE.
 *
 * PHP-MELANGE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-MELANGE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-MELANGE; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category    Melange
 * @package     php-melange
 * @subpackage  cms
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 *
 */

require_once("cms/core/action/AbstractListAction.php");

class ListAction extends AbstractListAction {

    private $log;

    public function __construct() {
        $this->log =& LoggerManager::getLogger("ListAction");
    }

    public function activate(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $this->log->debug("Activating pages");
        $c = new Criteria();
        $c->add(MenuPeer::MENU_ID, $form->getItems(), Criteria::IN);
        $c->add(MenuPeer::ACTIVE, true);
        try {
            MenuPeer::doUpdate($c);
            $this->messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.success.activate", true));
	        // Handle cache
	        if($request->getContext()->getInitParameter(_Core::CACHE)) {
	            // Get the cache
	            $cache = $request->getContext()->getCache();
	            // Clear the menu cache
	            $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('menu'));              
	            // Clear the page cache
	            $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('page'));
	        }
        } catch(PropelException $e) {
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.activate", true));
            $this->log->error($e->getMessage());
        }

    }

    public function deactivate(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $this->log->debug("Deactivating pages");
        $c = new Criteria();
        $c->add(MenuPeer::MENU_I18N_ID, $form->getItems(), Criteria::IN);
        $c->add(MenuPeer::ACTIVE, false);
        try {
            MenuPeer::doUpdate($c);
            $this->messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.success.deactivate", true));
	        // Handle cache
	        if($request->getContext()->getInitParameter(_Core::CACHE)) {
	            // Get the cache
	            $cache = $request->getContext()->getCache();
	            // Clear the menu cache
	            $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('menu'));              
	            // Clear the page cache
	            $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('page'));
	        }
        } catch(PropelException $e) {
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.deactivate", true));
            $this->log->error($e->getMessage());
        }

    }

    public function delete(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $this->log->debug("Deleting menus");

        $c = new Criteria();
        $c->add(MenuPeer::MENU_ID, $form->getItems(), Criteria::IN);

        try {

            $menus = MenuPeer::doDelete($c);
            $this->messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.success.delete", true));
	        // Handle cache
	        if($request->getContext()->getInitParameter(_Core::CACHE)) {
	            // Get the cache
	            $cache = $request->getContext()->getCache();
	            // Clear the menu cache
	            $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('menu'));              
	            // Clear the page cache
	            $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('page'));
	        }
        
        } catch(PropelException $e) {
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.delete", true));
            $this->log->error($e->getMessage());
        }

    }

}
?>